package webs;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.pojo.Replys;
import dao.pojo.Topics;
import domain.ApplicationContext;
import domain.BoardFacade;

public class GetTopic extends HttpServlet{
	
	private BoardFacade boardService;
	
	public BoardFacade getBoardService() {
		return boardService;
	}

	public void setBoardService(BoardFacade boardService) {
		this.boardService = boardService;
	}

    @Override
    protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        doPost(httpServletRequest,httpServletResponse);
    }

    @Override
    protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        
    	httpServletRequest.setCharacterEncoding("UTF-8");
    	BoardFacade boardService=(BoardFacade)ApplicationContext.get("BoardService");
    	
    	Integer topics_id = new Integer(httpServletRequest.getParameter("topic_id"));
    	Topics topic = new Topics();
    	topic.setId(topics_id);
    	topic = boardService.getTopic(topic);
    	ArrayList<Replys> replyList = boardService.getAllReplys(topic);
    	HttpSession session = httpServletRequest.getSession();
    	session.setAttribute("replyList", replyList);
    	session.setAttribute("topic", topic);
    	httpServletResponse.sendRedirect("reply.jsp");   	
    }

    @Override
    public void destroy() {
        super.destroy();    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void init() throws ServletException {
        super.init();    //To change body of overridden methods use File | Settings | File Templates.
    }
}
